import { createStore } from 'vuex'
import persistedState from 'vuex-persistedstate'
import { getRouteTitleHandled, routeHasExist } from '@/utils/tools'

export default createStore({
  state: {
    userInfo: null,
    collapsed: false, // 菜单默认展开
    role: [],
    permission: [],
    menuAuth: [], // 左栏菜单
    tagNaves: []
  },
  getters: {
  },
  mutations: {
    setUserInfo (state, info) {
      state.userInfo = info
    },
    setCollapsed (state, collapsed) {
      state.collapsed = collapsed
    },
    setMenuAuth (state, menu) {
      state.menuAuth = menu
    },
    setPermission (state, permission) {
      state.permission = permission
    },
    setRole (state, role) {
      state.role = role
    },
    setTagNaves (state, list) {
      state.tagNaves = list
    },
    addTag (state, addTag) {
      const { route, type = 'unshife' } = addTag
      const router = getRouteTitleHandled(route)
      if (!routeHasExist(state.tagNaves, router)) {
        if (type === 'push') state.tagNaves.push(router)
        else state.tagNaves.unshift(router)
      }
    }
  },
  actions: {

  },
  modules: {
  },
  plugins: [
    persistedState({ storage: window.sessionStorage })
  ]
})
